package com.zbkj.service.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.UserModelUrl;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.service.dao.UserModelUrlDao;
import com.zbkj.service.service.ProductService;
import com.zbkj.service.service.SystemAttachmentService;
import com.zbkj.service.service.UserModelUrlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author keNan
 * @description UserModelUrlServiceImpl 接口实现
 * @date 2025-05-13
 */
@Slf4j
@Service
public class UserModelUrlServiceImpl extends ServiceImpl<UserModelUrlDao, UserModelUrl> implements UserModelUrlService {

    @Autowired
    private SystemAttachmentService systemAttachmentService;
    @Autowired
    private ProductService productService;


    /**
     * 用户模特地址列表
     *
     * @param pageParamRequest
     * @return
     */
    @Override
    public PageInfo<UserModelUrl> getList(PageParamRequest pageParamRequest) {
        Page<UserModelUrl> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<UserModelUrl> list = list();
        return CommonPage.copyPageInfo(page, list);
    }


    /**
     * 查询用户模特地址列表
     *
     * @return
     */
    @Override
    public List<UserModelUrl> selectList() {
        return list();
    }


    /**
     * 查询模特地址
     *
     * @param modelInt
     * @return
     */
    @Override
    public UserModelUrl selectByModelType(Integer modelInt) {
        LambdaQueryWrapper<UserModelUrl> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserModelUrl::getModelType, modelInt);
        lqw.last("limit 1");
        UserModelUrl userModelUrl = getOne(lqw);
        if (userModelUrl == null || StrUtil.isBlank(userModelUrl.getModelUrl())) {
            String modelImages = "";
            switch (modelInt) {
                case 0:
                    modelImages = "https://py-1305242679.cos.ap-guangzhou.myqcloud.com/tryon/2025/05/08/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20250508171559.png";
                    break;
                case 1:
                    modelImages = "https://py-1305242679.cos.ap-guangzhou.myqcloud.com/tryon/2025/05/08/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20250507175541%20%282%29.png";
                    break;
                case 2:
                    modelImages = "https://py-1305242679.cos.ap-guangzhou.myqcloud.com/tryon/2025/05/08/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20250508171559.png";
                    break;
            }
            userModelUrl=new UserModelUrl();
            userModelUrl.setModelUrl(modelImages);
        }
        return userModelUrl;
    }


    /**
     * 新增用户模特地址
     *
     * @param userModelUrl
     * @return
     */
    @Override
    public Boolean addUserModelUrl(UserModelUrl userModelUrl) {
        userModelUrl.setCreateTime(new Date());
        userModelUrl.setModelUrl(systemAttachmentService.clearPrefix(userModelUrl.getModelUrl()));
        boolean b = save(userModelUrl);
        try {
            productService.createMask(userModelUrl);
        } catch (Exception e) {

        }
        return b;
    }


    /**
     * 修改用户模特地址
     *
     * @param userModelUrl
     * @return
     */
    @Override
    public Boolean updateUserModelUrlById(UserModelUrl userModelUrl) {
        userModelUrl.setUpdateTime(new Date());
        userModelUrl.setModelUrl(systemAttachmentService.clearPrefix(userModelUrl.getModelUrl()));
        boolean b = updateById(userModelUrl);
        try {
            productService.createMask(userModelUrl);
        } catch (Exception e) {

        }
        return b;
    }


    /**
     * 根据模特地址查询模特对象
     *
     * @param modelImgUrl
     * @return
     */
    @Override
    public UserModelUrl getOneByModelImgUrl(String modelImgUrl) {
        LambdaQueryWrapper<UserModelUrl> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserModelUrl::getModelUrl, modelImgUrl);
        lqw.last("limit 1");
        return getOne(lqw);
    }


}

